<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>RocksDB（四）：WAL Practice | WLeoo's Blog</title><meta name="keywords" content="RocksDB,数据库"><meta name="author" content="WLeoo,3049458366@qq.com"><meta name="copyright" content="WLeoo"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="WAL Practice在上一篇里已经完成了对于WAL的理论的学习，接下来让我们对于RocksDB中的WAL进行实际操作一番。 Tools for WAL我是在ubuntu上安装的RocksDB，我采用的编辑器是Clion。如果是在标准的linux操作系统，编译好RocksDB代码后会有一个ldb工具，可以用于查看WAL。这里具体对于ldb不介绍，之前已介绍过RocksDB相关工具的使用。  Cr">
<meta property="og:type" content="article">
<meta property="og:title" content="RocksDB（四）：WAL Practice">
<meta property="og:url" content="https://gitee.com/WLeoo/wleoo.git/2022/12/07/RocksDB%EF%BC%88%E5%9B%9B%EF%BC%89%EF%BC%9AWAL-Practice/index.html">
<meta property="og:site_name" content="WLeoo&#39;s Blog">
<meta property="og:description" content="WAL Practice在上一篇里已经完成了对于WAL的理论的学习，接下来让我们对于RocksDB中的WAL进行实际操作一番。 Tools for WAL我是在ubuntu上安装的RocksDB，我采用的编辑器是Clion。如果是在标准的linux操作系统，编译好RocksDB代码后会有一个ldb工具，可以用于查看WAL。这里具体对于ldb不介绍，之前已介绍过RocksDB相关工具的使用。  Cr">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070733489.jpg">
<meta property="article:published_time" content="2022-12-06T20:58:44.000Z">
<meta property="article:modified_time" content="2022-12-06T23:46:22.770Z">
<meta property="article:author" content="WLeoo">
<meta property="article:tag" content="RocksDB">
<meta property="article:tag" content="数据库">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070733489.jpg"><link rel="shortcut icon" href="/WLeoo/"><link rel="canonical" href="https://gitee.com/WLeoo/wleoo.git/2022/12/07/RocksDB%EF%BC%88%E5%9B%9B%EF%BC%89%EF%BC%9AWAL-Practice/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/WLeoo/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/WLeoo/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: {"limitDay":365,"position":"top","messagePrev":"It has been","messageNext":"days since the last update, the content of the article may be outdated."},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: WLeoo","link":"链接: ","source":"来源: WLeoo's Blog","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'RocksDB（四）：WAL Practice',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-12-07 07:46:22'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/WLeoo/css/wleoo.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/WLeoo/img/avatar.png" onerror="onerror=null;src='/img/ali.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/WLeoo/archives/"><div class="headline">文章</div><div class="length-num">13</div></a></div><div class="data-item"><a href="/WLeoo/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="data-item"><a href="/WLeoo/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/WLeoo/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/WLeoo/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/WLeoo/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/WLeoo/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/WLeoo/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/WLeoo/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/WLeoo/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/WLeoo/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/WLeoo/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070733489.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/WLeoo/">WLeoo's Blog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/WLeoo/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/WLeoo/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/WLeoo/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/WLeoo/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/WLeoo/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/WLeoo/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/WLeoo/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/WLeoo/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/WLeoo/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">RocksDB（四）：WAL Practice</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-12-06T20:58:44.000Z" title="发表于 2022-12-07 04:58:44">2022-12-07</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-12-06T23:46:22.770Z" title="更新于 2022-12-07 07:46:22">2022-12-07</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/WLeoo/categories/RocksDB/">RocksDB</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/WLeoo/categories/RocksDB/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">学习笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="RocksDB（四）：WAL Practice"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="WAL-Practice"><a href="#WAL-Practice" class="headerlink" title="WAL Practice"></a>WAL Practice</h1><p>在上一篇里已经完成了对于WAL的理论的学习，接下来让我们对于RocksDB中的WAL进行实际操作一番。</p>
<h2 id="Tools-for-WAL"><a href="#Tools-for-WAL" class="headerlink" title="Tools for WAL"></a>Tools for WAL</h2><p>我是在ubuntu上安装的RocksDB，我采用的编辑器是Clion。如果是在标准的linux操作系统，编译好RocksDB代码后会有一个<code>ldb</code>工具，可以用于查看WAL。<br>这里具体对于ldb不介绍，之前已介绍过RocksDB相关工具的使用。</p>
<p><img src="https://s1.ax1x.com/2022/11/30/zwTa5R.png" alt="enter image description here"></p>
<h2 id="Create-WAL"><a href="#Create-WAL" class="headerlink" title="Create WAL"></a>Create WAL</h2><p>创建WAL一共有两种情况：</p>
<ul>
<li>当一个新的数据库被打开</li>
<li>当一个列族被flush</li>
</ul>
<h3 id="Open-DB"><a href="#Open-DB" class="headerlink" title="Open DB"></a>Open DB</h3><p>首先当一个新的DB被打开时，就会创建一个WAL。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">s &#x3D; rocksdb::DB::Open(options, &quot;.&#x2F;db&#x2F;&quot;, &amp;db);</span><br></pre></td></tr></table></figure>

<p>下面我们对源码进行查看。在db_impl_open.cc文件，我们可以看到：DB::Open调用时会传入列族（column_families，以下可能简称CF）：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Status DB::Open(const DBOptions&amp; db_options, const std::string&amp; dbname,  </span><br><span class="line"> const std::vector&lt;ColumnFamilyDescriptor&gt;&amp; column_families,  </span><br><span class="line">  std::vector&lt;ColumnFamilyHandle*&gt;* handles, DB** dbptr)&#123;</span><br><span class="line">  const bool kSeqPerBatch &#x3D; true;  </span><br><span class="line">const bool kBatchPerTxn &#x3D; true;  </span><br><span class="line">return DBImpl::Open(db_options, dbname, column_families, handles, dbptr,  </span><br><span class="line">  !kSeqPerBatch, kBatchPerTxn);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>当我们打开DB时：</p>
<ul>
<li>  在恢复完MANIFEST之后，需要先获取即将创建的WAL 文件编号，即xxx.log</li>
<li>  为wal文件分配预分配 一个memtale的buffer大小 <code>max_write_buffer_size</code>，如果没有配置该选项，则会将wal可写入的大小定为memtable的4倍。</li>
<li>创建wal文件<br>  下面进入DBImpl::Open函数查看，</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">Status DBImpl::Open(const DBOptions&amp; db_options, const std::string&amp; dbname,  </span><br><span class="line"> const std::vector&lt;ColumnFamilyDescriptor&gt;&amp; column_families,  </span><br><span class="line">  std::vector&lt;ColumnFamilyHandle*&gt;* handles, DB** dbptr,  </span><br><span class="line"> const bool seq_per_batch, const bool batch_per_txn) &#123;</span><br><span class="line"> ......</span><br><span class="line">s &#x3D; impl-&gt;Recover(column_families, false, false, false, &amp;recovered_seq,  </span><br><span class="line">  &amp;recovery_ctx);  </span><br><span class="line">if (s.ok()) &#123;  </span><br><span class="line">  uint64_t new_log_number &#x3D; impl-&gt;versions_-&gt;NewFileNumber();</span><br><span class="line">  log::Writer* new_log &#x3D; nullptr;  </span><br><span class="line">const size_t preallocate_block_size &#x3D;  </span><br><span class="line">    impl-&gt;GetWalPreallocateBlockSize(max_write_buffer_size);  </span><br><span class="line">s &#x3D; impl-&gt;CreateWAL(new_log_number, 0 &#x2F;*recycle_log_number*&#x2F;,  </span><br><span class="line">  preallocate_block_size, &amp;new_log);</span><br><span class="line">  ......</span><br><span class="line">  &#125;</span><br><span class="line">  ......</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>


<h3 id="Flush-Column-Family"><a href="#Flush-Column-Family" class="headerlink" title="Flush Column Family"></a>Flush Column Family</h3><p>还有一种情况时当一个列族（column_family）被刷新到磁盘之后，也会创建新的WAL。<br>当column family flush的时候，通过DBImpl::Flush 调用对应cf的memtable flush函数，在flush memtable的过程中进行新的wal的创建。<br>这里当触发cf的flush时，需要将内存中memtable 标记为immutable-memetable，来进行后台的写入sst文件；同时会生成新的memtable，这个时候wal记录的是旧的memtable的请求，为了数据的隔离性，且wal不会过大，每个wal文件只和一个memtable绑定，所以切换memtable的过程中会创建新的WAL文件，用来接收新的请求。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">Status DBImpl::Flush(const FlushOptions&amp; flush_options,  </span><br><span class="line">  ColumnFamilyHandle* column_family) &#123;  </span><br><span class="line">  ...... </span><br><span class="line">  Status s; </span><br><span class="line">  &#x2F;&#x2F;执行flush memtable </span><br><span class="line"> if (immutable_db_options_.atomic_flush) &#123;  </span><br><span class="line">    s &#x3D; AtomicFlushMemTables(&#123;cfh-&gt;cfd()&#125;, flush_options,  </span><br><span class="line">  FlushReason::kManualFlush);  </span><br><span class="line">  &#125; else &#123;  </span><br><span class="line">    s &#x3D; FlushMemTable(cfh-&gt;cfd(), flush_options, FlushReason::kManualFlush);  </span><br><span class="line">  &#125;  </span><br><span class="line"> .......</span><br><span class="line">&#125;  </span><br><span class="line"></span><br><span class="line">Status DBImpl::FlushMemTable(ColumnFamilyData* cfd,  </span><br><span class="line"> const FlushOptions&amp; flush_options,  </span><br><span class="line">  FlushReason flush_reason, bool writes_stopped) &#123;  </span><br><span class="line">  &#x2F;&#x2F; This method should not be called if atomic_flush is true.  </span><br><span class="line">  ......</span><br><span class="line">  if (flush_reason !&#x3D; FlushReason::kErrorRecoveryRetryFlush &amp;&amp;  </span><br><span class="line">        (!cfd-&gt;mem()-&gt;IsEmpty() || !cached_recoverable_state_empty_.load())) &#123;</span><br><span class="line">      &#x2F;&#x2F;通过SwitchMemtable函数来进行memtable的切换  </span><br><span class="line">      s &#x3D; SwitchMemtable(cfd, &amp;context);  </span><br><span class="line">  &#125;  </span><br><span class="line">  ......</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>这种情况下创建新的WAL是通过SwitchMemtable函数。这个函数主要是用来切换memtable，也就是在执行flush之前所做的切换——生成新的memtable，然后把老的刷新到磁盘。查看db_impl_write.cc文件。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context) &#123;</span><br><span class="line">......</span><br><span class="line">&#x2F;&#x2F;前面获取到需要创建的wal 文件编号，预分配足够的wal存储空间，那么接下来开始进行对应的文件创建</span><br><span class="line">if (creating_new_log) &#123;  </span><br><span class="line">  &#x2F;&#x2F; TODO: Write buffer size passed in should be max of all CF&#39;s instead  </span><br><span class="line">  &#x2F;&#x2F; of mutable_cf_options.write_buffer_size.  </span><br><span class="line">  io_s &#x3D; CreateWAL(new_log_number, recycle_log_number, preallocate_block_size,  </span><br><span class="line">  &amp;new_log);  </span><br><span class="line">&#125;</span><br><span class="line">......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>通过上面的两个函数我们可以看到每次新建WAL都会有一个new_log_number，这个值就是对应的WAL的文件名前缀，可以看到每次生成新的log_number， 基本都会调用NewFileNumber函数.这里注意如果option设置了recycle_log_file_num的话，是有可能重用老的log_number的。我们先来看下NewFileNumber函数:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">uint64_t NewFileNumber() &#123; return next_file_number_.fetch_add(1); &#125;</span><br></pre></td></tr></table></figure>

<p>可以看到函数实现很简单，就是每次log_number加一，因此一般来说WAL的文件格式都是类似0000001.log这样子。</p>
<h2 id="Update-WAL"><a href="#Update-WAL" class="headerlink" title="Update WAL"></a>Update WAL</h2><p>关于WAL的更新部分，主要讲解的时在对memtable 进行flush时的WAL的更新做更为详细的描述。<br>WAL的更新时上面所说的两种情形，出发cf的flush和达到wal文件大小限制时，此时会发生：</p>
<ul>
<li>新的cf中的数据会刷到的新的SST文件之中，即SwitchMemtable，将当前的memtable变为immutable memtable，然后immutable memtable在后台进行flush</li>
<li>创建一个新的WAL文件，之后所有的cf的write请求会优先写入新的WAL之中。在SwitchMemtable中调用CreateWAL。</li>
<li>旧的WAL文件会被标记为不可写入，并且在之后会删除（并没有立刻删除，上一篇有讲解删除的过程）。而这个过程就是在CreateWAL中进行的。</li>
</ul>
<p>下面我们用一个简单的demo进行测试。测试的完整代码会放在后面。<br><strong>第一次测试思路</strong>：首先打开一个db，向两个column family中分别写入四组key-value，然后再触发一次flush，只flush一个CF。<br><strong>第一次结果预期</strong>：在我们第一次flush时应当会存在两个WAL文件，一个时open时自动创建的，一个时我们进行第一次flush时新创建的新的WAL，并且因为我们只进行了一次flush并且数据量较小，所以应该还会有一个sst文件。<br>因为flush时切换memtable时创建了新的WAL文件，所以新产生的WAL文件应该是空的。之前的数据插入应当被保存在第一个WAL文件中。而又因为我们只flush了cf1，cf0的数据还在内存中，所以第一个WAL的文件是不能被删除的。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">s &#x3D; rocksdb::DB::Open(options, &quot;.&#x2F;dbtest&quot;, column_families, &amp;handles, &amp;db);  </span><br><span class="line">  </span><br><span class="line">cout &lt;&lt; handles.size() &lt;&lt; &quot; open status is : &quot; &lt;&lt; s.ToString()  &lt;&lt; endl;  </span><br><span class="line">db-&gt;Put(rocksdb::WriteOptions(), handles[1], rocksdb::Slice(&quot;key1&quot;), rocksdb::Slice(&quot;value1&quot;));  </span><br><span class="line">db-&gt;Put(rocksdb::WriteOptions(), handles[0], rocksdb::Slice(&quot;key2&quot;), rocksdb::Slice(&quot;value2&quot;));  </span><br><span class="line">db-&gt;Put(rocksdb::WriteOptions(), handles[1], rocksdb::Slice(&quot;key3&quot;), rocksdb::Slice(&quot;value3&quot;));  </span><br><span class="line">db-&gt;Put(rocksdb::WriteOptions(), handles[0], rocksdb::Slice(&quot;key4&quot;), rocksdb::Slice(&quot;value4&quot;));  </span><br><span class="line">  </span><br><span class="line">db-&gt;Flush(rocksdb::FlushOptions(), handles[1]);</span><br></pre></td></tr></table></figure>

<p>运行上述代码后我们进行查看，结果如下：<br><img src="https://s1.ax1x.com/2022/12/02/zBdBGR.png" alt="enter image description here"><br>可以看到存在两个log（WAL）文件，一个SST文件。经检查000010.log存在四组kv写入的记录，而000014.log为空。SST中保存了（k1,v1）,（k3,v3）的值。结果展示如下。<br><img src="https://s1.ax1x.com/2022/12/02/zBserq.png" alt="enter image description here"><br>通过第一次的flush，我们可以了解到测试结果基本如我们所预料。结合相应的源码，我们也弄清了关于WAL文件切换更新的过程。</p>
<ul>
<li>打开db时会创建一个WAL文件</li>
<li>flush memtable的时候也会创建一个WAL文件，当旧的WAL文件中存在没有被flush的cf的数据时它暂时不会被删除，知道所有的cf的数据都被flush到sst文件中才会被删除。这是正常场景保证数据一直性的WAL实现，接接下来我们进行第二次测试。<br><strong>第二次测试思路</strong>：在第一次测试的基础上进行再一次输入kv，写入未被flush的cf，并进行第二次flush。<br><strong>第二次结果预测</strong>：因为我们进行了两次flush，所以此时应当具有三个WAL文件和两个SST文件。第一个WAL文件记录着第一次实验四组kv（1~4）的写入，由于全部被flush完毕，所以应当被删除。然后第二WAL文件应当记录着cf0新的两组kv（5、6）的写入，由于也被flush完毕，所以也应当被删除了，第三个WAL文件时第二次flush后新产生的，所以应该为空。因此最终只剩下一个log文件。而第一个SST应当包含着四组kv，第二个SST应当时新写入的两组kv。<br>代码如下：</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; key5 and key6 will appear in a new WAL  </span><br><span class="line">db-&gt;Put(rocksdb::WriteOptions(), handles[0], rocksdb::Slice(&quot;key5&quot;), rocksdb::Slice(&quot;value5&quot;));  </span><br><span class="line">db-&gt;Put(rocksdb::WriteOptions(), handles[0], rocksdb::Slice(&quot;key6&quot;), rocksdb::Slice(&quot;value6&quot;));  </span><br><span class="line">  </span><br><span class="line">db-&gt;Flush(rocksdb::FlushOptions(), handles[0]);</span><br></pre></td></tr></table></figure>

<p>运行上述代码后我们进行查看，结果如下：<br><img src="https://s1.ax1x.com/2022/12/03/zDzjmj.png" alt="enter image description here">可以看到只存在一个log（WAL）文件，一个SST文件。经检查000016.log为空。000015.sst中保存了（k1,v1）,（k3,v3）的值，000017.sst中保存了（k2，v2），（k4，v4），（k5，v5），（k6，v6）。结果展示如下。<br><img src="https://s1.ax1x.com/2022/12/03/zrS1je.png" alt="enter image description here"><br>两次测试完毕，结果均如预期所示。下面贴出完整的测试代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;  </span><br><span class="line">#include &lt;string&gt;  </span><br><span class="line">#include &lt;vector&gt;  </span><br><span class="line">#include &lt;rocksdb&#x2F;db.h&gt;  </span><br><span class="line">#include &lt;rocksdb&#x2F;iterator.h&gt;  </span><br><span class="line">#include &lt;rocksdb&#x2F;table.h&gt;  </span><br><span class="line">#include &lt;rocksdb&#x2F;options.h&gt;  </span><br><span class="line">  </span><br><span class="line">using std::cout;  </span><br><span class="line">using std::endl;  </span><br><span class="line">  </span><br><span class="line">int main () &#123;  </span><br><span class="line">  rocksdb::DB* db;  </span><br><span class="line">  rocksdb::Options options;  </span><br><span class="line">  rocksdb::Status s;  </span><br><span class="line">  </span><br><span class="line">  std::vector&lt;rocksdb::ColumnFamilyDescriptor&gt; column_families;  </span><br><span class="line">  column_families.push_back(rocksdb::ColumnFamilyDescriptor(  </span><br><span class="line">      rocksdb::kDefaultColumnFamilyName, rocksdb::ColumnFamilyOptions()));  </span><br><span class="line">  column_families.push_back(rocksdb::ColumnFamilyDescriptor(  </span><br><span class="line">      &quot;new_cf&quot;, rocksdb::ColumnFamilyOptions()));  </span><br><span class="line">  std::vector&lt;rocksdb::ColumnFamilyHandle*&gt; handles;  </span><br><span class="line">  </span><br><span class="line">  options.create_if_missing &#x3D; true;  </span><br><span class="line">  options.max_open_files &#x3D; -1;  </span><br><span class="line">  </span><br><span class="line">  s &#x3D; rocksdb::DB::Open(options, &quot;.&#x2F;dbtest&#x2F;&quot;, &amp;db);  </span><br><span class="line">  cout &lt;&lt; &quot;db open status is : &quot; &lt;&lt; s.ok() &lt;&lt; endl;  </span><br><span class="line">  </span><br><span class="line">  &#x2F;&#x2F; create column family  </span><br><span class="line">  rocksdb::ColumnFamilyHandle* cf;  </span><br><span class="line">  s &#x3D; db-&gt;CreateColumnFamily(rocksdb::ColumnFamilyOptions(), &quot;new_cf&quot;, &amp;cf);  </span><br><span class="line">  cout &lt;&lt; &quot;create CF is : &quot; &lt;&lt; s.ok() &lt;&lt; endl;  </span><br><span class="line">  </span><br><span class="line">  &#x2F;&#x2F; close DB  </span><br><span class="line">  s &#x3D; db-&gt;DestroyColumnFamilyHandle(cf);  </span><br><span class="line">  cout &lt;&lt; &quot;destroy CF is : &quot; &lt;&lt; s.ok() &lt;&lt; endl;  </span><br><span class="line">  &#x2F;&#x2F;delete db;  </span><br><span class="line">  </span><br><span class="line">  s &#x3D; rocksdb::DB::Open(options, &quot;.&#x2F;dbtest&quot;, column_families, &amp;handles, &amp;db);  </span><br><span class="line">  cout &lt;&lt; &quot;db open again status is : &quot; &lt;&lt; s.ok() &lt;&lt; endl;  </span><br><span class="line">  </span><br><span class="line">  cout &lt;&lt; handles.size() &lt;&lt; &quot; open status is : &quot; &lt;&lt; s.ToString()  &lt;&lt; endl;  </span><br><span class="line">  db-&gt;Put(rocksdb::WriteOptions(), handles[1], rocksdb::Slice(&quot;key1&quot;), rocksdb::Slice(&quot;value1&quot;));  </span><br><span class="line">  db-&gt;Put(rocksdb::WriteOptions(), handles[0], rocksdb::Slice(&quot;key2&quot;), rocksdb::Slice(&quot;value2&quot;));  </span><br><span class="line">  db-&gt;Put(rocksdb::WriteOptions(), handles[1], rocksdb::Slice(&quot;key3&quot;), rocksdb::Slice(&quot;value3&quot;));  </span><br><span class="line">  db-&gt;Put(rocksdb::WriteOptions(), handles[0], rocksdb::Slice(&quot;key4&quot;), rocksdb::Slice(&quot;value4&quot;));  </span><br><span class="line">  </span><br><span class="line">  db-&gt;Flush(rocksdb::FlushOptions(), handles[1]);  </span><br><span class="line">  </span><br><span class="line">  &#x2F;&#x2F; key5 and key6 will appear in a new WAL  </span><br><span class="line">  db-&gt;Put(rocksdb::WriteOptions(), handles[0], rocksdb::Slice(&quot;key5&quot;), rocksdb::Slice(&quot;value5&quot;));  </span><br><span class="line">  db-&gt;Put(rocksdb::WriteOptions(), handles[0], rocksdb::Slice(&quot;key6&quot;), rocksdb::Slice(&quot;value6&quot;));  </span><br><span class="line">  </span><br><span class="line">  db-&gt;Flush(rocksdb::FlushOptions(), handles[0]);  </span><br><span class="line">  </span><br><span class="line"> delete db;  </span><br><span class="line">  </span><br><span class="line"> return 0;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行指令如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wl@wl-ubuntu:~&#x2F;rocksdb&#x2F;rocksdb-7.7.3&#x2F;mytest$ g++ -std&#x3D;c++17 -o waltest waltest.cpp -lrocksdb  -lpthread -ldl</span><br><span class="line">wl@wl-ubuntu:~&#x2F;rocksdb&#x2F;rocksdb-7.7.3&#x2F;mytest$ .&#x2F;waltest</span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意，我看到很多人在介绍运行指令是所用的都是C++11这个版本。但当我实际操作时发现会出现类似“‘string_view’ in namespace ‘std’ does not name a type”这种报错。经我查阅这是C++版本的问题，只需要将C++版本从11换成17即可。C++11具体报错贴图如下：<br><img src="https://s1.ax1x.com/2022/12/03/zrSX8K.png" alt="enter image description here"></p>
</blockquote>
<h2 id="Recover-WAL"><a href="#Recover-WAL" class="headerlink" title="Recover WAL"></a>Recover WAL</h2><p>在之前我们就有这样学习过，RocksDB对于WAL的恢复时提供了可供调整的四种WAL Recovery模式。这里简单解释一下：</p>
<ul>
<li>kAbsoluteConsistency<br>这种级别是对一致性要求最高的级别，不允许有任何的IO错误，不能出现一个record的丢失。</li>
<li>kTolerateCorruptedTailRecords<br>这个级别是允许丢失一部分数据，会忽略一些在wal末尾写入失败的请求，数据异常仅限于log文件末尾写入失败。如果出现了其他的异常，都无法进行数据重放。</li>
<li>kPointInTimeRecovery<br>这个级别也是现在rocksdb默认的recovery mode，当遇到IO error的时候会停止重放，将出现异常之前的所有数据进行完成重放。</li>
<li>kSkipAnyCorruptedRecords<br>这个级别是一致性要求最低的，会忽略所有的IO error，尝试尽可能多得恢复数据。一般用于灾难性丢失的恢复。<br>在文件options.h中，对于这四种模式是这样声明的：</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">enum class WALRecoveryMode : char &#123;  </span><br><span class="line"> &#x2F;&#x2F; Original levelDB recovery  </span><br><span class="line"> &#x2F;&#x2F; We tolerate the last record in any log to be incomplete due to a crash &#x2F;&#x2F; Use case: Applications for which updates, once applied, must not be  </span><br><span class="line"> &#x2F;&#x2F; rolled back even after a crash-recovery.  </span><br><span class="line"> kTolerateCorruptedTailRecords &#x3D; 0x00,  </span><br><span class="line"> &#x2F;&#x2F; Recover from clean shutdown  </span><br><span class="line"> &#x2F;&#x2F; We don&#39;t expect to find any corruption in the WAL </span><br><span class="line"> &#x2F;&#x2F; Use case : This is ideal for unit tests and rare applications that </span><br><span class="line"> &#x2F;&#x2F; can require high consistency guarantee  </span><br><span class="line"> kAbsoluteConsistency &#x3D; 0x01,  </span><br><span class="line"> &#x2F;&#x2F; Recover to point-in-time consistency (default)  </span><br><span class="line"> &#x2F;&#x2F; We stop the WAL playback on discovering WAL inconsistency </span><br><span class="line"> &#x2F;&#x2F; Use case : Ideal for systems that have disk controller cache like </span><br><span class="line"> &#x2F;&#x2F; hard disk, SSD without super capacitor that store related data  </span><br><span class="line"> kPointInTimeRecovery &#x3D; 0x02,  </span><br><span class="line"> &#x2F;&#x2F; Recovery after a disaster  </span><br><span class="line"> &#x2F;&#x2F; We ignore any corruption in the WAL and try to salvage as much data as </span><br><span class="line"> &#x2F;&#x2F; possible </span><br><span class="line"> &#x2F;&#x2F; Use case : Ideal for last ditch effort to recover data or systems that </span><br><span class="line"> &#x2F;&#x2F; operate with low grade unrelated data  </span><br><span class="line"> kSkipAnyCorruptedRecords &#x3D; 0x03,  </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>因此通过上述配置，我们可以通过option选项<code>Options.wal_recovery_mode = 2</code>来进行相应模式的配置。<br>接下来我们来仔细查看一下每一种Recovery Mode时如何实现各自的Recovery的级别的。<br>当我们打开db调用Recovery时，如果db已经存在，RocksDB会尝试从已经存在的db中的log文件来恢复上一次的memtable数据：<br>详细的过程如下：</p>
<ul>
<li>读取存在的每一个log文件，循环进行如下操作</li>
<li>创建一个WAL文件的file_reader</li>
<li>利用file_reader创建一个log reader，并利用wal_recovery_mode参与到log reader的初始化</li>
<li>在具体的ReadRecord中进行对应的Recovery Mode区分<br>在源文件db_impl_open.cc中，可以查看到函数<code>c RecoverLogFiles</code>定义如下：</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">Status DBImpl::RecoverLogFiles(const std::vector&lt;uint64_t&gt;&amp; wal_numbers,  </span><br><span class="line">  SequenceNumber* next_sequence, bool read_only,  </span><br><span class="line"> bool* corrupted_wal_found,  </span><br><span class="line">  RecoveryContext* recovery_ctx) &#123;</span><br><span class="line">......</span><br><span class="line">&#x2F;&#x2F;遍历所有的wal record，wal_numbers为所有wal的log文件列表  </span><br><span class="line">&#x2F;&#x2F;这个函数可以处理多个wal文件，wal文件列表记录在参数wal_numbers中  </span><br><span class="line">&#x2F;&#x2F;这个循环遍历所有的wal_numbers  </span><br><span class="line">for (auto wal_number : wal_numbers) &#123;</span><br><span class="line">......</span><br><span class="line">&#x2F;&#x2F;先得到wal文件名，在使用SequentialFileReader打开wal文件，再使用这个reader构造log::Reader</span><br><span class="line">std::string fname &#x3D;  </span><br><span class="line">    LogFileName(immutable_db_options_.GetWalDir(), wal_number);</span><br><span class="line">......</span><br><span class="line">&#x2F;&#x2F;Create the file_reader</span><br><span class="line">std::unique_ptr&lt;SequentialFileReader&gt; file_reader;  </span><br><span class="line">&#123;  </span><br><span class="line">  std::unique_ptr&lt;FSSequentialFile&gt; file;  </span><br><span class="line">  status &#x3D; fs_-&gt;NewSequentialFile(fname,  fs_-&gt;OptimizeForLogRead(file_options_),  &amp;file,nullptr);  </span><br><span class="line">  ......</span><br><span class="line">  file_reader.reset(new SequentialFileReader(  </span><br><span class="line">      std::move(file), fname, immutable_db_options_.log_readahead_size,  </span><br><span class="line">  io_tracer_));  </span><br><span class="line">&#125;</span><br><span class="line">......</span><br><span class="line">&#x2F;&#x2F; Create the log reader</span><br><span class="line">log::Reader reader(immutable_db_options_.info_log, std::move(file_reader),  </span><br><span class="line">  &amp;reporter, true &#x2F;*checksum*&#x2F;, wal_number);</span><br><span class="line">&#125;</span><br><span class="line">......</span><br><span class="line">&#x2F;&#x2F;循环处理所有的record，每次循环处理一个record</span><br><span class="line">while (!stop_replay_by_wal_filter &amp;&amp;  </span><br><span class="line">       reader.ReadRecord(&amp;record, &amp;scratch,  </span><br><span class="line">  immutable_db_options_.wal_recovery_mode,  </span><br><span class="line">  &amp;record_checksum) &amp;&amp;  </span><br><span class="line">       status.ok()) &#123;  </span><br><span class="line">  ......  </span><br><span class="line">  if (immutable_db_options_.wal_recovery_mode &#x3D;&#x3D;  </span><br><span class="line">      WALRecoveryMode::kPointInTimeRecovery) &#123;  </span><br><span class="line">		if (sequence &#x3D;&#x3D; *next_sequence) &#123;  </span><br><span class="line">      stop_replay_for_corruption &#x3D; false;  </span><br><span class="line">  &#125;  </span><br><span class="line">    if (stop_replay_for_corruption) &#123;  </span><br><span class="line">      logFileDropped();  </span><br><span class="line"> break;  &#125;  </span><br><span class="line">  &#125;  </span><br><span class="line">  &#x2F;&#x2F;解析record并把其中的kv组装成batch  </span><br><span class="line">	WriteBatch batch;</span><br><span class="line">  ......  </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>所以我们接下来需要进入到<code>ReadRecord</code>中，从物理Record中读取数据发生异常时，如果想要上报异常，我们都需要确认当前Recovery Mode是最高一致性<code>kAbsoluteConsistency</code>的时候才会进行上报。<br>在log_reader.cc文件中查看<code>ReadRecord</code>函数定义：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">bool Reader::ReadRecord(Slice* record, std::string* scratch,  </span><br><span class="line">  WALRecoveryMode wal_recovery_mode,  </span><br><span class="line">  uint64_t* record_checksum) &#123;</span><br><span class="line">  uint64_t physical_record_offset &#x3D; end_of_buffer_offset_ - buffer_.size();</span><br><span class="line">    size_t drop_size &#x3D; 0;</span><br><span class="line">    &#x2F;&#x2F; 会从物理的record中读取数据,将读取过程中发生的异常返回给record_type</span><br><span class="line">    const unsigned int record_type &#x3D; ReadPhysicalRecord(&amp;fragment, &amp;drop_size);</span><br><span class="line">	......</span><br><span class="line">	switch (record_type) &#123;</span><br><span class="line">	......</span><br><span class="line">	&#x2F;&#x2F;接下来通过不同的recovery mode来针对异常的record_type 进行处理，决定是否需要返回数据异常</span><br><span class="line">	&#x2F;&#x2F;当出现 header,eof(log末尾)，kOldRecord（和eof异常类似）异常时都会 确认recovery mode是否是 kAbsoluteConsistency</span><br><span class="line">	&#x2F;&#x2F;是的话直接report异常</span><br><span class="line">      case kBadHeader:</span><br><span class="line">        if (wal_recovery_mode &#x3D;&#x3D; WALRecoveryMode::kAbsoluteConsistency) &#123;</span><br><span class="line">          &#x2F;&#x2F; in clean shutdown we don&#39;t expect any error in the log files</span><br><span class="line">          ReportCorruption(drop_size, &quot;truncated header&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        ......</span><br><span class="line">      case kEof:</span><br><span class="line">        if (in_fragmented_record) &#123;</span><br><span class="line">          if (wal_recovery_mode &#x3D;&#x3D; WALRecoveryMode::kAbsoluteConsistency) &#123;</span><br><span class="line">            &#x2F;&#x2F; in clean shutdown we don&#39;t expect any error in the log files</span><br><span class="line">            ReportCorruption(scratch-&gt;size(), &quot;error reading trailing data&quot;);</span><br><span class="line">          &#125;</span><br><span class="line">          ......</span><br><span class="line">      case kOldRecord:</span><br><span class="line">        if (wal_recovery_mode !&#x3D; WALRecoveryMode::kSkipAnyCorruptedRecords) &#123;</span><br><span class="line">          &#x2F;&#x2F;如果是跳过所有异常的话，不会report corruption</span><br><span class="line">          &#x2F;&#x2F; Treat a record from a previous instance of the log as EOF.</span><br><span class="line">          if (in_fragmented_record) &#123;</span><br><span class="line">            if (wal_recovery_mode &#x3D;&#x3D; WALRecoveryMode::kAbsoluteConsistency) &#123;</span><br><span class="line">              &#x2F;&#x2F; in clean shutdown we don&#39;t expect any error in the log files</span><br><span class="line">              ReportCorruption(scratch-&gt;size(), &quot;error reading trailing data&quot;);</span><br><span class="line">            &#125;</span><br><span class="line">            &#x2F;&#x2F; This can be caused by the writer dying immediately after</span><br><span class="line">            &#x2F;&#x2F;  writing a physical record but before completing the next; don&#39;t</span><br><span class="line">            &#x2F;&#x2F;  treat it as a corruption, just ignore the entire logical record.</span><br><span class="line">            scratch-&gt;clear();</span><br><span class="line">          &#125;</span><br><span class="line">          return false;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>紧接着我们重新回到RecoverLogFiles函数之中，在处理完Record之后，后续还会有将重放后的数据写入memtable的过程。<br>当完成所有的读取和写入memtable之后，会对所有的返回状态进行确认，因为之上所有的操作都是在重放wal,可能会有失败的情况。<br>接下来进入如下的逻辑，根据重放后的状态进行后续的recovery操作（以下将<code>immutable_db_options_.wal_recovery_mode</code>用mode来代替）</p>
<ul>
<li>如果mode是 kSkipAnyCorruptedRecords ，则跳过所有的异常，直接返回OK</li>
<li>如果mode是 kPointInTimeRecovery 或者 kTolerateCorruptedTailRecords 则会暂停处理，将stop_replay_for_corruption 置为true, 后续会跳过当前的log number进行重放</li>
<li>如果是kAbsoluteConsistency 或者 kTolerateCorruptedTailRecords 则直接返回</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">    if (immutable_db_options_.wal_recovery_mode &#x3D;&#x3D;</span><br><span class="line">        WALRecoveryMode::kSkipAnyCorruptedRecords) &#123; &#x2F;&#x2F; 如果rev</span><br><span class="line">      &#x2F;&#x2F; We should ignore all errors unconditionally</span><br><span class="line">      status &#x3D; Status::OK();</span><br><span class="line">    &#125; else if (immutable_db_options_.wal_recovery_mode &#x3D;&#x3D;</span><br><span class="line">               WALRecoveryMode::kPointInTimeRecovery) &#123;</span><br><span class="line">      &#x2F;&#x2F; We should ignore the error but not continue replaying</span><br><span class="line">      status &#x3D; Status::OK();</span><br><span class="line">      stop_replay_for_corruption &#x3D; true;</span><br><span class="line">      corrupted_log_number &#x3D; log_number;</span><br><span class="line">      ROCKS_LOG_INFO(immutable_db_options_.info_log,</span><br><span class="line">                     &quot;Point in time recovered to log #%&quot; PRIu64</span><br><span class="line">                     &quot; seq #%&quot; PRIu64,</span><br><span class="line">                     log_number, *next_sequence);</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      assert(immutable_db_options_.wal_recovery_mode &#x3D;&#x3D;</span><br><span class="line">                 WALRecoveryMode::kTolerateCorruptedTailRecords ||</span><br><span class="line">             immutable_db_options_.wal_recovery_mode &#x3D;&#x3D;</span><br><span class="line">                 WALRecoveryMode::kAbsoluteConsistency);</span><br><span class="line">      return status;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 主要是对kPointInTimeRecovery 和 kTolerateCorruptedTailRecords进行恢复处理</span><br><span class="line">&#x2F;&#x2F; 将corrupted_log_number 之前所有数据完成恢复，再报告异常</span><br><span class="line">if (stop_replay_for_corruption &#x3D;&#x3D; true &amp;&amp;</span><br><span class="line">    (immutable_db_options_.wal_recovery_mode &#x3D;&#x3D;</span><br><span class="line">         WALRecoveryMode::kPointInTimeRecovery ||</span><br><span class="line">     immutable_db_options_.wal_recovery_mode &#x3D;&#x3D;</span><br><span class="line">         WALRecoveryMode::kTolerateCorruptedTailRecords)) &#123;</span><br><span class="line">  for (auto cfd : *versions_-&gt;GetColumnFamilySet()) &#123;</span><br><span class="line">    if (cfd-&gt;GetLogNumber() &gt; corrupted_log_number) &#123;</span><br><span class="line">      ROCKS_LOG_ERROR(immutable_db_options_.info_log,</span><br><span class="line">                      &quot;Column family inconsistency: SST file contains data&quot;</span><br><span class="line">                      &quot; beyond the point of corruption.&quot;);</span><br><span class="line">      return Status::Corruption(&quot;SST file is ahead of WALs&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Write-to-WAL"><a href="#Write-to-WAL" class="headerlink" title="Write to WAL"></a>Write to WAL</h2><p>接下来我们补充一下WAL文件时如何在写入memtable之前旧写入的，又是如何进行更新的。<br>还是通过用户接口<code>Put</code>，该接口会调用到底层的<code>Status DBImpl::WriteImpl</code>，在该函数中，如果我们开启来pipeline，则会走pipeline的写入逻辑。如果未开启pipeline，则会正常写入。</p>
<blockquote>
<p>pipeline是流水线的意思，可以类比成指令流水线的思想。</p>
</blockquote>
<p>在源文件db_impl_open.cc中，<code>WriteToWAL</code>的函数入口如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">io_s &#x3D;  </span><br><span class="line"> WriteToWAL(write_group, log_context.writer, log_used,  </span><br><span class="line"> log_context.need_log_sync, log_context.need_log_dir_sync,  </span><br><span class="line"> last_sequence + 1, log_file_number_size);</span><br></pre></td></tr></table></figure>

<p>因为实际写入的过程中可能多个客服端会并发调用put，而每一个put都会有自己的writer，为了提高写性能，会从多个writer中选出来一个leader，让这个leader将所有要写的wal收集到一块，进行batch写入，其他从writer等待leader写完之后在并发写入memtable。<br>所以以上<code>WriteToWAL</code>内部调用了一个重载 了batch写的wal函数。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">WriteBatch* merged_batch;  </span><br><span class="line">io_s &#x3D; status_to_io_status(MergeBatch(write_group, &amp;tmp_batch_, &amp;merged_batch,  </span><br><span class="line">  &amp;write_with_wal, &amp;to_be_cached_state));</span><br><span class="line"> ......</span><br><span class="line"> uint64_t log_size;  </span><br><span class="line">io_s &#x3D; WriteToWAL(*merged_batch, log_writer, log_used, &amp;log_size,  </span><br><span class="line">  write_group.leader-&gt;rate_limiter_priority,  </span><br><span class="line">  log_file_number_size);</span><br><span class="line">  if (to_be_cached_state) &#123;  </span><br><span class="line">  cached_recoverable_state_ &#x3D; *to_be_cached_state;  </span><br><span class="line">  cached_recoverable_state_empty_ &#x3D; false;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>所以进入到实际执行（也就是重寫的）的<code>WriteToWAL</code>函数内部，会调用AddRecord函数执行实际的write。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">IOStatus DBImpl::WriteToWAL(const WriteBatch&amp; merged_batch,  </span><br><span class="line">  log::Writer* log_writer, uint64_t* log_used,  </span><br><span class="line">  uint64_t* log_size,  </span><br><span class="line">  Env::IOPriority rate_limiter_priority,  </span><br><span class="line">  LogFileNumberSize&amp; log_file_number_size) &#123;</span><br><span class="line">  ......</span><br><span class="line">  &#x2F;&#x2F;調用AddRecord</span><br><span class="line">  IOStatus io_s &#x3D; log_writer-&gt;AddRecord(log_entry, rate_limiter_priority);</span><br><span class="line">	......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里会用到文件系统的write接口，选择的文件系统是在db open的时候 进行<code>DBImpl</code>类初始化，根据env传入的参数进行文件系统的选择， 在fs_posxi.cc文件中，我們可以看到默认是<code>PosixFileSystem</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">std::shared_ptr&lt;FileSystem&gt; FileSystem::Default() &#123;  </span><br><span class="line"> STATIC_AVOID_DESTRUCTION(std::shared_ptr&lt;FileSystem&gt;, instance)  </span><br><span class="line">  (std::make_shared&lt;PosixFileSystem&gt;());  </span><br><span class="line"> return instance;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在<code>AddRecord</code>函数中将之前合并好的 log_enery，每次写入大小不能超过11bytes，这个是WAL的具体约定的格式了。通过文件系统接口进行Append写入，直到把log_entry完全写入。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">IOStatus Writer::AddRecord(const Slice&amp; slice,  </span><br><span class="line">  Env::IOPriority rate_limiter_priority) &#123;</span><br><span class="line">	......</span><br><span class="line">	do &#123;</span><br><span class="line">	......</span><br><span class="line"> 	   s &#x3D; dest--&gt;Append(Slice(&quot;\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00&quot;,</span><br><span class="line">                            static_cast&lt;size_t&gt;(leftover)));</span><br><span class="line">	......</span><br><span class="line">	&#125;while(s.ok() &amp;&amp; left &gt; 0);</span><br><span class="line">	......</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>因为我们没有配置direct，则最终通过<br><code>Flush</code> –&gt;<code>WriteBuffered</code>–&gt; <code>PosixWritableFile::Append</code> –&gt; <code>PosixWrite</code> –&gt; <code>write</code> 直到底层的 write系统调用，进行写入。<br>其实这个写入链路还是比较长，不过耗时的话中间仅仅是做一些字符串的拼接，到write这里才是耗时所在。因为，还要走一遍庞大的内核文件系统的写入链路，这里也能够理解为什么ceph需要单独再做一个小型的文件系统来代替内核文件系统了。</p>
<p>写WAL是我们整个rocksdb写入最为耗时的一段，memtable的写入是在WAL写完成之后才能写入，而memtable都是纯内存操作，所以写入操作的耗时还是消耗在了写WAL之上，在传统的xfs之上，一个200bytes的请求，写WAL耗时大概2-4us。</p>
<p>为了保证WAL能够落盘，我们还需要配置options.sync=true,在<code>WriteToWAL</code>函数中，完成了文件系统的写入之后 会调用fsync来进行 sync写。</p>
<h2 id="Delete-WAL"><a href="#Delete-WAL" class="headerlink" title="Delete WAL"></a>Delete WAL</h2><h3 id="DeleteObsoleteFiles"><a href="#DeleteObsoleteFiles" class="headerlink" title="DeleteObsoleteFiles"></a>DeleteObsoleteFiles</h3><p>WAL的删除只有当包含在此WAL中的所有的数据都已经被持久化为SST之后(也有可能会延迟删除，因为有时候需要master发送transcation Log到slave来回放)。<br>首先在<code>Status DBImpl::Open</code>中会调用<code>DeleteObsoleteFiles</code>函数：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Status DBImpl::Open(const DBOptions&amp; db_options, const std::string&amp; dbname,......) &#123;</span><br><span class="line">......</span><br><span class="line">TEST_SYNC_POINT(&quot;DBImpl::Open:Opened&quot;);  </span><br><span class="line">Status persist_options_status;  </span><br><span class="line">if (s.ok()) &#123;  </span><br><span class="line">  ......</span><br><span class="line">  *dbptr &#x3D; impl;  </span><br><span class="line">  impl-&gt;opened_successfully_ &#x3D; true;  </span><br><span class="line">  impl-&gt;DeleteObsoleteFiles();  </span><br><span class="line">  TEST_SYNC_POINT(&quot;DBImpl::Open:AfterDeleteFiles&quot;);  </span><br><span class="line">  impl-&gt;MaybeScheduleFlushOrCompaction();  </span><br><span class="line">&#125; </span><br><span class="line">......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在<code>DeleteObsoleteFiles</code>函数则调用了两个非常重要的删除函数<code>FindObsoleteFiles</code>和<code>PurgeObsoleteFiles</code>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">void DBImpl::DeleteObsoleteFiles() &#123;  </span><br><span class="line"> ......</span><br><span class="line">  JobContext job_context(next_job_id_.fetch_add(1));  </span><br><span class="line">  FindObsoleteFiles(&amp;job_context, true);  </span><br><span class="line">  </span><br><span class="line">  mutex_.Unlock();  </span><br><span class="line"> if (job_context.HaveSomethingToDelete()) &#123;  </span><br><span class="line">    bool defer_purge &#x3D; immutable_db_options_.avoid_unnecessary_blocking_io;  </span><br><span class="line">  PurgeObsoleteFiles(job_context, defer_purge);  </span><br><span class="line">  &#125;  </span><br><span class="line">  .......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="FindObsoleteFiles"><a href="#FindObsoleteFiles" class="headerlink" title="FindObsoleteFiles"></a>FindObsoleteFiles</h3><p>在WAL的删除中这个函数的主要功能时扫描需要删除的文件，然后交给PurgeObsoleteFiles删除。如果rocksdb构建checkpoint的时候，会在这个函数里面直接return。<br><strong>这个函数会在全局锁的保护范围内，所以这个函数不能做太重的事情，否则会影响rocksdb的读写性能</strong><br>rocksdb默认6个小时会扫描db的全量文件，这个全局扫描在文件数特别多场景下会有性能问题。<br>经测试，当文件数达到百万级别的时候耗时就非常大了，所以不推荐单个rocksdb存储特别多的数据，当然可以通过调整文件大小来减少文件数，但是这样做也会有其他的性能问题。<br>至于FindObsoleteFiles函数为什么要全量扫描文件，猜测可能更多的是为了防止代码出现bug导致文件不能被删除。<br>同时在这个函数里面会获取每个cf最小的log文件号，然后判断logs_和alive_log_files_里面是否有文件需要删除。<br>在源文件db_impl_files.cc中我们先来看DBImpl::FIndObsoleteFiles函数。这个函数很长，我们只关注对应的WAL部分，这里逻辑很简单，就是遍历所有的WAL，然后找出log_number小于当前min_log_number的文件然后加入到对应的结构(log_delete_files)。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">if (!alive_log_files_.empty() &amp;&amp; !logs_.empty()) &#123;  </span><br><span class="line">  uint64_t min_log_number &#x3D; job_context-&gt;log_number;  </span><br><span class="line">  size_t num_alive_log_files &#x3D; alive_log_files_.size();  </span><br><span class="line">  &#x2F;&#x2F; 查看最新的过时日志文件  </span><br><span class="line">  while (alive_log_files_.begin()-&gt;number &lt; min_log_number) &#123;  </span><br><span class="line">    auto&amp; earliest &#x3D; *alive_log_files_.begin();  </span><br><span class="line"> if (immutable_db_options_.recycle_log_file_num &gt;  </span><br><span class="line">        log_recycle_files_.size()) &#123;  </span><br><span class="line">      ROCKS_LOG_INFO(immutable_db_options_.info_log,  </span><br><span class="line">  &quot;adding log %&quot; PRIu64 &quot; to recycle list\n&quot;,  </span><br><span class="line">  earliest.number);  </span><br><span class="line">  log_recycle_files_.push_back(earliest.number);  </span><br><span class="line">  &#125; else &#123;  </span><br><span class="line">      job_context-&gt;log_delete_files.push_back(earliest.number);  </span><br><span class="line">  &#125;  </span><br><span class="line">  ......</span><br><span class="line">  &#125;</span><br><span class="line"> while (!logs_.empty() &amp;&amp; logs_.front().number &lt; min_log_number) &#123;  </span><br><span class="line">    auto&amp; log &#x3D; logs_.front();  </span><br><span class="line"> if (log.IsSyncing()) &#123;  </span><br><span class="line">      log_sync_cv_.Wait();  </span><br><span class="line">  &#x2F;&#x2F; logs_ could have changed while we were waiting.  </span><br><span class="line">  continue;  </span><br><span class="line">  &#125;  </span><br><span class="line">    logs_to_free_.push_back(log.ReleaseWriter());  </span><br><span class="line">  logs_.pop_front();  </span><br><span class="line">  &#125;  </span><br><span class="line">  &#x2F;&#x2F; 当前的日志文件不可能过时  </span><br><span class="line">  assert(!logs_.empty());  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里可以看到有两个核心的数据结构alive_log_files和logs_，他们的区别就是前一个只表示有写入的WAL，而后一个则是包括了所有的WAL(比如open一个DB,而没有写入数据，此时也会生成WAL)。<br>最终删除WAL的操作是在DBImpl::DeleteObsoleteFileImpl这个函数。而WAL删除不会单独触发，而是和temp/sst这类文件一起被删除的(PurgeObsoleteFiles)。</p>
<h3 id="PurgeObsoleteFiles"><a href="#PurgeObsoleteFiles" class="headerlink" title="PurgeObsoleteFiles"></a>PurgeObsoleteFiles</h3><p>在FindOnsoleteFiles函数中，构造好的<code>job_context</code>传出到清理函数<code>PurgeObsoleteFiles</code>即可。<br>这个函数主要执行最终的删除，会二次校验文件是否可以删除，通知也支持异步删除。<br>将文件名中列出的文件和不属于实时文件可能会被删除的文件进行比较。此外，删除sst_delete_files和log_delete_files中的所有文件。调用此方法时不必持有互斥锁。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">void DBImpl::PurgeObsoleteFiles(JobContext&amp; state, bool schedule_only) &#123;</span><br><span class="line">......</span><br><span class="line">&#x2F;&#x2F;获取需要删除的WAL</span><br><span class="line">std::unordered_set&lt;uint64_t&gt; log_recycle_files_set(  </span><br><span class="line">    state.log_recycle_files.begin(), state.log_recycle_files.end());</span><br><span class="line">......</span><br><span class="line">for (const auto&amp; candidate_file : candidate_files) &#123;</span><br><span class="line">......</span><br><span class="line">switch (type) &#123;  </span><br><span class="line">  case kWalFile:  </span><br><span class="line">    keep &#x3D; ((number &gt;&#x3D; state.log_number) ||  </span><br><span class="line">            (number &#x3D;&#x3D; state.prev_log_number) ||  </span><br><span class="line">            (log_recycle_files_set.find(number) !&#x3D;  </span><br><span class="line">  log_recycle_files_set.end()));  </span><br><span class="line"> break; </span><br><span class="line"> case kDescriptorFile:  </span><br><span class="line">    keep &#x3D; (sst_live_set.find(number) !&#x3D; sst_live_set.end()) ||  </span><br><span class="line">           number &gt;&#x3D; state.min_pending_output;  </span><br><span class="line"> if (!keep) &#123;  </span><br><span class="line">      files_to_del.insert(number);  </span><br><span class="line">  &#125;  </span><br><span class="line">    break;</span><br><span class="line">......</span><br><span class="line">&#125;</span><br><span class="line">......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a target="_blank" rel="noopener" href="https://wleoo.gitee.io/">诗酒</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a target="_blank" rel="noopener" href="https://wleoo.gitee.io/">https://wleoo.gitee.io/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">此文章版权归诗酒所有，如有转载，请註明来自原作者</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/WLeoo/tags/RocksDB/">RocksDB</a><a class="post-meta__tags" href="/WLeoo/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a></div><div class="post_share"><div class="social-share" data-image="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070733489.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/WLeoo/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/WLeoo/img/wechat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/WLeoo/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/WLeoo/img/alipay.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/WLeoo/2022/12/07/OpenEC%EF%BC%9A%E9%9D%A2%E5%90%91%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F%E7%9A%84%E7%BB%9F%E4%B8%80%E5%8F%AF%E9%85%8D%E7%BD%AE%E6%93%A6%E9%99%A4%E7%BC%96%E7%A0%81%E7%AE%A1%E7%90%86/"><img class="prev-cover" src="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070742533.jpg" onerror="onerror=null;src='/WLeoo/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">OpenEC：面向分布式系统的统一可配置擦除编码管理</div></div></a></div><div class="next-post pull-right"><a href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E4%B8%89%EF%BC%89%EF%BC%9AWAL-Learning/"><img class="next-cover" src="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070729354.jpg" onerror="onerror=null;src='/WLeoo/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">RocksDB（三）：WAL Learning</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E4%B8%89%EF%BC%89%EF%BC%9AWAL-Learning/" title="RocksDB（三）：WAL Learning"><img class="cover" src="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070729354.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-12-07</div><div class="title">RocksDB（三）：WAL Learning</div></div></a></div><div><a href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E4%BA%8C%EF%BC%89%EF%BC%9AAdministration-and-Data-Access-Tool/" title="RocksDB（二）：Administration and Data Access Tool"><img class="cover" src="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070727412.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-12-07</div><div class="title">RocksDB（二）：Administration and Data Access Tool</div></div></a></div><div><a href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E4%B8%80%EF%BC%89%EF%BC%9AWhat-is-RocksDB/" title="RocksDB（一）：What is RocksDB"><img class="cover" src="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070624447.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-12-07</div><div class="title">RocksDB（一）：What is RocksDB</div></div></a></div><div><a href="/WLeoo/2022/04/16/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AE%9E%E9%AA%8C%E6%8A%A5%E5%91%8A%E4%B8%80/" title="《数据库系统原理》实验报告一"><img class="cover" src="https://s2.loli.net/2022/04/29/M3dOgNthEqaFSDy.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-04-16</div><div class="title">《数据库系统原理》实验报告一</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div id="comment-switch"><span class="first-comment">Valine</span><span class="switch-btn"></span><span class="second-comment">Disqus</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="disqus_thread"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/WLeoo/img/avatar.png" onerror="this.onerror=null;this.src='/WLeoo/img/ali.gif'" alt="avatar"/></div><div class="author-info__name">WLeoo</div><div class="author-info__description">但行善举，莫问前程</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/WLeoo/archives/"><div class="headline">文章</div><div class="length-num">13</div></a></div><div class="card-info-data-item"><a href="/WLeoo/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item"><a href="/WLeoo/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/WLeoo"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/WLeoo" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="/WLeoo/3049458366@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#WAL-Practice"><span class="toc-number">1.</span> <span class="toc-text">WAL Practice</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Tools-for-WAL"><span class="toc-number">1.1.</span> <span class="toc-text">Tools for WAL</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Create-WAL"><span class="toc-number">1.2.</span> <span class="toc-text">Create WAL</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Open-DB"><span class="toc-number">1.2.1.</span> <span class="toc-text">Open DB</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Flush-Column-Family"><span class="toc-number">1.2.2.</span> <span class="toc-text">Flush Column Family</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Update-WAL"><span class="toc-number">1.3.</span> <span class="toc-text">Update WAL</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Recover-WAL"><span class="toc-number">1.4.</span> <span class="toc-text">Recover WAL</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Write-to-WAL"><span class="toc-number">1.5.</span> <span class="toc-text">Write to WAL</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Delete-WAL"><span class="toc-number">1.6.</span> <span class="toc-text">Delete WAL</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#DeleteObsoleteFiles"><span class="toc-number">1.6.1.</span> <span class="toc-text">DeleteObsoleteFiles</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#FindObsoleteFiles"><span class="toc-number">1.6.2.</span> <span class="toc-text">FindObsoleteFiles</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#PurgeObsoleteFiles"><span class="toc-number">1.6.3.</span> <span class="toc-text">PurgeObsoleteFiles</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/WLeoo/2022/12/07/OpenEC%EF%BC%9A%E9%9D%A2%E5%90%91%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F%E7%9A%84%E7%BB%9F%E4%B8%80%E5%8F%AF%E9%85%8D%E7%BD%AE%E6%93%A6%E9%99%A4%E7%BC%96%E7%A0%81%E7%AE%A1%E7%90%86/" title="OpenEC：面向分布式系统的统一可配置擦除编码管理"><img src="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070742533.jpg" onerror="this.onerror=null;this.src='/WLeoo/img/404.jpg'" alt="OpenEC：面向分布式系统的统一可配置擦除编码管理"/></a><div class="content"><a class="title" href="/WLeoo/2022/12/07/OpenEC%EF%BC%9A%E9%9D%A2%E5%90%91%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F%E7%9A%84%E7%BB%9F%E4%B8%80%E5%8F%AF%E9%85%8D%E7%BD%AE%E6%93%A6%E9%99%A4%E7%BC%96%E7%A0%81%E7%AE%A1%E7%90%86/" title="OpenEC：面向分布式系统的统一可配置擦除编码管理">OpenEC：面向分布式系统的统一可配置擦除编码管理</a><time datetime="2022-12-06T23:41:12.000Z" title="发表于 2022-12-07 07:41:12">2022-12-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E5%9B%9B%EF%BC%89%EF%BC%9AWAL-Practice/" title="RocksDB（四）：WAL Practice"><img src="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070733489.jpg" onerror="this.onerror=null;this.src='/WLeoo/img/404.jpg'" alt="RocksDB（四）：WAL Practice"/></a><div class="content"><a class="title" href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E5%9B%9B%EF%BC%89%EF%BC%9AWAL-Practice/" title="RocksDB（四）：WAL Practice">RocksDB（四）：WAL Practice</a><time datetime="2022-12-06T20:58:44.000Z" title="发表于 2022-12-07 04:58:44">2022-12-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E4%B8%89%EF%BC%89%EF%BC%9AWAL-Learning/" title="RocksDB（三）：WAL Learning"><img src="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070729354.jpg" onerror="this.onerror=null;this.src='/WLeoo/img/404.jpg'" alt="RocksDB（三）：WAL Learning"/></a><div class="content"><a class="title" href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E4%B8%89%EF%BC%89%EF%BC%9AWAL-Learning/" title="RocksDB（三）：WAL Learning">RocksDB（三）：WAL Learning</a><time datetime="2022-12-06T20:38:42.000Z" title="发表于 2022-12-07 04:38:42">2022-12-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E4%BA%8C%EF%BC%89%EF%BC%9AAdministration-and-Data-Access-Tool/" title="RocksDB（二）：Administration and Data Access Tool"><img src="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070727412.jpg" onerror="this.onerror=null;this.src='/WLeoo/img/404.jpg'" alt="RocksDB（二）：Administration and Data Access Tool"/></a><div class="content"><a class="title" href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E4%BA%8C%EF%BC%89%EF%BC%9AAdministration-and-Data-Access-Tool/" title="RocksDB（二）：Administration and Data Access Tool">RocksDB（二）：Administration and Data Access Tool</a><time datetime="2022-12-06T20:32:00.000Z" title="发表于 2022-12-07 04:32:00">2022-12-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E4%B8%80%EF%BC%89%EF%BC%9AWhat-is-RocksDB/" title="RocksDB（一）：What is RocksDB"><img src="https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070624447.png" onerror="this.onerror=null;this.src='/WLeoo/img/404.jpg'" alt="RocksDB（一）：What is RocksDB"/></a><div class="content"><a class="title" href="/WLeoo/2022/12/07/RocksDB%EF%BC%88%E4%B8%80%EF%BC%89%EF%BC%9AWhat-is-RocksDB/" title="RocksDB（一）：What is RocksDB">RocksDB（一）：What is RocksDB</a><time datetime="2022-12-06T19:23:41.000Z" title="发表于 2022-12-07 03:23:41">2022-12-07</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://blog-imgs-1315248452.cos.ap-nanjing.myqcloud.com/images/202212070733489.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By WLeoo</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">Hi, welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="rightside.chat_btn"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/WLeoo/js/utils.js"></script><script src="/WLeoo/js/main.js"></script><script src="/WLeoo/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/WLeoo/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'FxBfa6mfRNowzptPvsvHni4L-gzGzoHsz',
      appKey: '5PkFiNt3LOiRXfEDGFsGov5p',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: true
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script>function loadDisqus () {
  var disqus_config = function () {
    this.page.url = 'https://gitee.com/WLeoo/wleoo.git/2022/12/07/RocksDB%EF%BC%88%E5%9B%9B%EF%BC%89%EF%BC%9AWAL-Practice/'
    this.page.identifier = '2022/12/07/RocksDB（四）：WAL-Practice/'
    this.page.title = 'RocksDB（四）：WAL Practice'
  };

  window.disqusReset = () => {
    DISQUS.reset({
      reload: true,
      config: disqus_config
    })
  }

  if (window.DISQUS) disqusReset()
  else {
    (function() { 
      var d = document, s = d.createElement('script');
      s.src = 'https://.disqus.com/embed.js';
      s.setAttribute('data-timestamp', +new Date());
      (d.head || d.body).appendChild(s);
    })();
  }
}

if ('Valine' === 'Disqus' || !false) {
  if (false) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
  else loadDisqus()
} else {
  function loadOtherComment () {
    loadDisqus()
  }
}
</script></div><script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/gh/xiabo2/CDN@latest/fishes.js"></script><div class="aplayer no-destroy" data-id="60198" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="false"> </div><script src="/WLeoo/js/wleoo.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function history_calendar_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div>';
                console.log('已挂载history_calendar')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            history_calendar_injector_config()
        } </script><script data-pjax  src="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.js"></script><script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script><!-- hexo injector body_end end --></body></html>